package com.xxx.store.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xxx.store.entity.UserSelect;
import com.xxx.store.service.UserSelectService;
import com.xxx.store.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2023-04-27
 */
@RestController
@RequestMapping("/userSelect")
public class UserSelectController extends BaseController<UserSelect, UserSelectService> {

    @Autowired
    UserSelectService userSelectService;

    @Override
    public Result add(@RequestBody UserSelect userSelect) {
        // 判断这个用户之前有没有参与
        int count = userSelectService.count(new QueryWrapper<UserSelect>().eq("user_id", userSelect.getUserId())
                .eq("vote_id", userSelect.getVoteId()));
        if (count > 0) {
            return Result.error("您已经参与过本次投票！");
        } else {
            userSelectService.save(userSelect);
            return Result.ok("投票成功");
        }
    }
}

